package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.dto.PictureAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class PictureServiceImpl implements IPictureService {
    @Autowired
    PictureMapper pictureMapper;
    public PictureServiceImpl() {
        log.debug("创建业务类：PictureServiceImpl");
    }
    @Override
    public void andNew(PictureAndNewDTO pictureAndNewDTO) {
        log.debug("开始处理添加业务:参数：{}",pictureAndNewDTO);
        LocalDateTime now=LocalDateTime.now();
        Picture picture=new Picture();
        BeanUtils.copyProperties(pictureAndNewDTO,picture);
        picture.setGmtCreate(now);
        picture.setGmtModified(now);
        int insert = pictureMapper.insert(picture);
         if (insert!=1){
             String message="添加数据失败！服务器忙，请稍后再一次尝试!";
             log.debug(message);
             throw new ServiceException(ServiceCode.ERR_INSERT,message);
         }
    }
    @Override
    public void delete(Long id){
    log.debug("删除功能开始id：{}",id);
        PictureStandardVO pictureMapperById = pictureMapper.getById(id);
        if (pictureMapperById==null){
            String message = "删除数据失败！该数据"+id+"不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i=pictureMapper.deleteById(id);
        if (i!=1){
            String message="删除数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
